<template>

    <div class="dashboard-container">
        <div id="container">
            <el-form :inline="true" style="margin-left: 15px;">
                <el-form-item label="名称"><el-input v-model="category" placeholder="请输入分类名"></el-input></el-form-item>
                <el-button type="primary" @click="pageQuery()" icon="el-icon-search"
                    v-loading.fullscreen.lock="fullscreenLoading">查询</el-button>
                <el-button type="primary" icon="el-icon-close" @click="reset()">重置</el-button>
                <el-button type="primary" icon="el-icon-plus el-icon-plus" @click="dialogFormVisible2 = true">新增</el-button>
            </el-form>
        </div>

        <el-table :data="tableData" border style="width: 100%; margin-left: 15px;">
            <el-table-column prop="id" label="ID" width="50">
            </el-table-column>
            <el-table-column prop="category" label="分类名称" width="180">
            </el-table-column>
            <el-table-column prop="remark" label="备注">
            </el-table-column>
            <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" icon="el-icon-edit el-icon" @click="sel(scope.row.id),dialogFormVisible3=true">编辑</el-button>
                    <el-button type="primary" size="small" icon="el-icon-delete-solid" @click="del(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination align="center" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="page" :background="background" :page-sizes="[5, 10, 15, 20]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
        </el-pagination>


        <div class="background-color">
        <el-dialog title="分类添加" :visible.sync="dialogFormVisible2">
            <el-form :model="GoodsCategory" :rules="rules" ref="addForm">
                <el-form-item label="分类" :label-width="dialogFormVisible2" prop="category">
                    <el-input v-model="GoodsCategory.category" class="size"></el-input>
                </el-form-item>
                <el-form-item label="备注" :label-width="dialogFormVisible2" prop="remark">
                    <el-input v-model="GoodsCategory.remark" class="size"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="add">确 定</el-button>
            </div>
        </el-dialog>
    </div>

        <!-- 修改 -->
        <div class="background-color">
        <el-dialog title="修改" :visible.sync="dialogFormVisible3">
            <el-form :model="GoodsCategory" :rules="rules" ref="editForm">
                <el-form-item label="分类" :label-width="dialogFormVisible3" prop="category">
                    <el-input v-model="GoodsCategory.category" class="size" placeholder="请输入分类名称"></el-input>
                </el-form-item>
                <el-form-item label="备注" :label-width="dialogFormVisible3" prop="remark">
                    <el-input v-model="GoodsCategory.remark" class="size" placeholder="请输入备注"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible3 = false,GoodsCategory.category=null,GoodsCategory.remark=null">取 消</el-button>
                <el-button type="primary" @click="upd">确 定</el-button>
            </div>
        </el-dialog>
    </div>
 </div>
</template>

<script>
import { page, getById, selectById,addCategory,update} from '@/api/GoodsCategory.js'
export default {
    data() {
        const validateCategory = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入分类名称'));
            } else if (value.length > 10) {
                callback(new Error('分类名称不能超过10个字符'));
            } else {
                callback();
            }
        };

        const validateRemark = (rule, value, callback) => {
            if (value && value.length > 50) {
                callback(new Error('备注不能超过50个字符'));
            } else {
                callback();
            }
        };

        return {
            id: null,
            category: null,
            remark: null,
            page: 1,
            pageSize: 10,
            tableData: [],
            GoodsCategory:{
                id: null,
                category: '',
                remark: '',
            },
            total: 0,
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogFormVisible2: false,
            dialogFormVisible3:false,
            formLabelWidth: '120px',
             rules: {
                category: [
                    { required: true, message: '分类名称不能为空', trigger: 'blur' },
                    { validator: validateCategory, trigger: 'blur' }
                ],
                remark: [
                    { validator: validateRemark, trigger: 'blur' }
                ]
            }
        }
    },

    //服务启动后自动执行分页查询
    created() {
        this.pageQuery();
    },

    methods: {
        //分页查询
        pageQuery() {
            const params = {
                id: this.id,
                category: this.category,
                remerk: this.remerk,
                page: this.page,
                pageSize: this.pageSize
            }
            page(params).then(res => {
                this.total = res.data.data.total
                this.tableData = res.data.data.records
            })
        },

        //重置
        reset() {
            this.category = ''
            this.pageQuery();
        },

        add() {
            this.$refs.addForm.validate(valid => {
                if (valid) {
                    addCategory(this.GoodsCategory).then(res => {
                        //添加成功
                        this.$message.success("添加成功");
                        
                        //添加成功后表单取消
                        this.dialogFormVisible2 = false;

                        // 重置表单
                        this.$refs.addForm.resetFields();
                        this.GoodsCategory = {
                            id: null,
                            category: '',
                            remark: '',
                        };

                        this.pageQuery();

                        //重新查询数据
                        this.$router.push('/GoodsCategoryManage/GoodsCategoryManage');
                    }).catch(error => {
                        this.$message.error("添加失败: " + (error.response?.data?.message || error.message));
                    });
                } else {
                    return false;
                }
            });
        },


        //删除
        del(id) {
            this.$confirm(`此操作将永久删除ID=${id}的数据, 是否继续?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                getById(id)
                    .then(res => {
                        if (res.data.code === 1) {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            })
                        } else {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                            this.pageQuery()
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },

        //修改
        sel(id) {  
            selectById(id).then(res => {
                this.GoodsCategory = res.data.data;
                // 重置表单验证状态
                this.$nextTick(() => {
                    if (this.$refs.editForm) {
                        this.$refs.editForm.clearValidate();
                    }
                });
            });
        
    },
        upd() {
            this.$refs.editForm.validate(valid => {
                if (valid) {
                    update(this.GoodsCategory).then(res => {
                        this.$message.success("修改成功");
                        this.dialogFormVisible3 = false;
                        
                        this.pageQuery();
                    }).catch(error => {
                        this.$message.error("修改失败: " + (error.response?.data?.message || error.message));
                    });
                }
            });
        },
        },

        handleSizeChange(val) {
            this.pageSize = val
            this.pageQuery()
        },
        handleCurrentChange(val) {
            this.page = val
            this.pageQuery()
        },
        // 监控多选框事件
        handleSelectionChange(selection) {
            this.selectNum = selection.length;
            this.selection = selection;
        },

    }



</script>

<style scoped>
.dashboard-container {
    padding: 20px;
}

.size {
    width: 25%;
    min-width: 300px;
    
}

.el-table {
    margin-top: 20px;
}

.el-pagination {
    margin-top: 20px;
}
</style>